module.exports = app => {
    const express = require('express')
    const upload = require('../upload')
    const customer = require('../../models/customer')
    const goodslist = require('../../models/GoodsList')
    const router = express.Router({
        mergeParams: true
    })

    router.get('/goodslist',async(req,res) => {
        const data = await goodslist.find()
        res.send(data)
    })   
     router.delete('/goodslist',async(req,res) => {
       console.log( req.body.id )
     var remove = await goodslist.findOneAndRemove({_id:req.body.id})
     console.log(remove)
     const data = await goodslist.find() 
     res.send(data)
  })
    
    router.post('/upload', upload.single('file'), async(req,res)=>{
        res.send('/images/'+req.file.filename)
    })

    router.post('/goodslist',async(req,res)=>{
      const data = req.body
      const time = new Date()
      const model = {
        name: data.name,
        desc: data.desc,
        class: data.classes,
        specifications: data.specifications,
        unit:data.unit,
        area:data.area,
        shelves_time: time,
        price: Number(data.price).toFixed(2),
        oprice: Number(data.oprice).toFixed(2),
        status: '在售',
        goods_imgUrl: data.goods_imgUrl,
        detail_imgUrl: data.detail_imgUrl,
        goods_detail: data.goods_detail,
        value: 0,
        sq_fencheng: Number(data.fencheng).toFixed(2),
        phone:data.phone,
        number:data.number
      }
      await goodslist.create(model,(err,goods) => {
            err ? res.send('服务器错误...') : res.send(goods)
        })  
    })

    // router.post('/goodslist',upload.fields([{ name: 'goods_imgUrl', maxCount: 5 }, { name: 'detail_imgUrl', maxCount: 5 }]), async(req,res) => {
    //   const data = req.body
    //   console.log(data)
    //   const goods_imgUrl = []
    //   const detail_imgUrl = []
    //   for(var i = 0;i<req.files['goods_imgUrl'].length;i++){
    //     goods_imgUrl.push(req.files['goods_imgUrl'][i].filename)
    //   }
    //   for(var i = 0;i<req.files['detail_imgUrl'].length;i++){
    //     detail_imgUrl.push(req.files['detail_imgUrl'][i].filename)
    //   }
    //   const model = {
    //     name: data.name,
    //     desc: data.desc,
    //     class: data.class,
    //     specifications: data.specifications,
    //     shelvs_time: data.shelvs_time,
    //     number: data.number,
    //     numbers: data.numbers,
    //     price: data.price,
    //     oprice: data.oprice,
    //     status: data.status,
    //     goods_imgUrl: goods_imgUrl,
    //     detail_imgUrl: detail_imgUrl,
    //     goods_detail: data.goods_detail,
    //     value: 0
    //   }
    //   await goodslist.create(model,(err) => {
    //         err ? res.send('服务器错误...') : res.send(data)
    //     })  
    // })

    router.put('/goodslist/status',async(req,res) => {
      const { _id, status } = req.body
      await goodslist.updateOne({ _id: _id }, { $set: { status: status }},(err)=>{
        err ? res.send('服务器错误...') : res.send('ok')
      })
    })

    router.put('/goodslist/number',async(req,res) => {
      const { _id, number} = req.body
      await goodslist.updateOne({ _id: _id }, { $set: { number: number }},(err)=>{
        err ? res.send('服务器错误...') : res.send('ok')
      })
    })

    router.put('/goodslist/area',async(req,res) => {
      const { _id, area} = req.body
      await goodslist.updateOne({ _id: _id }, { $set: { area: area }},(err)=>{
        err ? res.send('服务器错误...') : res.send('ok')
      })
    })

     
    app.use('/api/admin',router)
    // app.use('/api/rest/:resource',async(req,res,next) => {
    //     const modelName = require('inflection').classify(req.params.resource)
    //     req.Model = require(`../../models/${modelName}`)
    //     next()
    // },router)

}